package com.xiaoma.myaudience.biz.task;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

import com.xiaoma.myaudience.biz.model.ActivityInfoModel;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.model.ModelUtils;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

/**
 * 异步加载筛选信息.
 * 
 */
public class ActivitySelectTask extends BaseDataAsyncTask<Map<String, String>, Void, Map<String, Object>>{
    private final static String TAG = "ActivitySelectTask";
    
    public ActivitySelectTask(Context context, int taskType, DataAsyncCallback callback) {
        super(context, taskType, callback);
    }

	@Override
	protected Map<String, Object> doInBackground(Map<String, String>... params) {
		Map<String, Object> result = null;
		Map<String, String> cookie = null;
		String eventId = null;
		if (params != null) {
			cookie = params[0];
		}
        try {            
        	String cookiestr = Constant.LOGIN_COOKIE1+"="+cookie.get(Constant.LOGIN_COOKIE1)+";"+Constant.LOGIN_COOKIE2+"="+cookie.get(Constant.LOGIN_COOKIE2);
			Header[] headers = new BasicHeader[2];//("X-Online-Host",  format_CMWAP_ServerName(strPath));  
			headers[0]=new BasicHeader("Cookie", cookiestr);
			headers[1]=new BasicHeader("Accept-Language","zh-CN");
			eventId = cookie.get("url");
			
			List<NameValuePair> values = new ArrayList<NameValuePair>();
			values.add(new BasicNameValuePair("op", "class"));
			values.add(new BasicNameValuePair("classid", cookie.get("classid")));
			values.add(new BasicNameValuePair("direction", "1"));
			values.add(new BasicNameValuePair("line", cookie.get("line")));
			values.add(new BasicNameValuePair("perpage", "10"));
			values.add(new BasicNameValuePair("province", cookie.get("province")));
			values.add(new BasicNameValuePair("city", cookie.get("city")));
			values.add(new BasicNameValuePair("time", cookie.get("time")));
			
			String str = HttpUtils.getHttpStringResult(getHttpClient(), Constant.URL_ACTIVITY_SELECT,
					values, headers, HttpUtils.POST, "GBK");
			
        	JSONObject jsonObject = new JSONObject(str);
        	if (jsonObject != null) {
        			Map<String, Object> objMap = ModelUtils.json2Map(jsonObject);
        			if (objMap != null) {
        				result = ModelUtils.getMapValue(objMap, ActivityInfoModel.PARAM_DATA);
					}
				}
        } catch (Exception e) {
            Logger.e(TAG, "doInBackground error!!!", e);
        } finally {
            closeHttpClient();
        }
        
        return result;	
    }
}
